
  /*
	@Author: 于辉
	@Time: 2020-11
	@Tittle: rolePowers
	@Description: 用户列表
*/
layui.use(['form','layer','jquery',"table"],function(){
 	var table = layui.table;
 	var layer = layui.layer;
    //渲染数据列表
	var renderTable = function (param){
		table.render({
			id: 'userID'
		    ,elem: '#userlist'
            ,even: true //开启隔行背景
		    ,url:getServerPath("GETUSETLIST")
		    ,method: 'post' 
		    ,contentType: 'application/json'
		    ,height: 'full-150'
		    ,headers:{SYSTOKEN: getSettings("token")}
		    ,where: {
			    "queryText": $("#search_input").val(),
				"orderBy": "createTime",
				"sort": "descending"
			} 
		    ,parseData: function(res){ //res 即为原始返回的数据
		    	console.log(res)
			    return {
			      "code": res.statusCode == 200?0:res.statusCode, //解析接口状态
			      "msg": res.message, //解析提示文本
			      "count": res.data.totalCount, //解析数据长度
			      "data": res.data.dataSource //解析数据列表
			    };
			}
		    ,cols: [[
		      {type:'checkbox'}
		      ,{field:'userID', width:80, title: 'ID',hide:true, sort: true}
		      ,{field:'userName', width:200, title: '用户名',align:"center"}
		      ,{field:'nickName', width:200, title: '昵称',align:"center"}
		      ,{field:'phone', width:150, title: '手机号',align:"center"}
		      ,{field:'sex', width:80, title: '性别',align:"center"}
		      ,{field:'createTime', width:150, title: '创建时间', sort: true,align:"center",templet:function(row){
		      	return new Date(row.createTime).Format("yyyy-MM-dd");
		      }}
		      ,{field:'enabled', width:150, title: '是否启用',align:"center",templet:function(row){
		      	if(row.enabled){
		      		return "启用";
		      	}else{
		      		return "禁用";
		      	}
		      	
		      }}
		      ,{field:'remark',  title: '备注',align:"center",templet:function(row){
		      	if(row.remark){
		      		return "<div class='align-left'>"+row.remark+"</div>";
		      	}
		      	return "";
		      }}
		      ,{field:'', width:300, title: '操作' ,align:"center" , templet:function(row){
		      	var html = '<button type="button" class="layui-btn layui-btn-sm" onclick="editInfo(\''+row.userID+'\')"><i class="layui-icon layui-icon-edit"></i>编辑</button>';
		      	if(row.enabled){
		      		html += '<button type="button" class="layui-btn layui-btn-danger layui-btn-sm" onclick="chengeUserEnable(\''+row.userID+'\',false)"><i class="iconfont gkb-jinyong"></i>禁用</button>';
		      	}else {
		      		html += '<button type="button" class="layui-btn layui-btn-normal layui-btn-sm" onclick="chengeUserEnable(\''+row.userID+'\',true)"><i class="iconfont gkb-shujubeifenhuifu"></i>恢复</button>';
		      	}
                html += '<button type="button" class="layui-btn layui-btn-warm layui-btn-sm" onclick="ResetPassword(\''+row.userID+'\')"><i class="iconfont gkb-chongzhimima"></i>重置密码</button>';

		      	return html;
		      }}
		    ]]
		    ,page: true
		});
        $('#page').css('text-align', 'right');  // 分页右对齐
        $('.layui-table-page').css('text-align', 'right');  // 数据表格分页右对齐
	}
	

    renderTable();
	 //搜索
    layui.$("#btn_search").on("click", function () {
        renderTable( );
    })
    //添加用户
    layui.$('#btn_add').on('click', function(){
	    //页面层-自定义
		layer.open({
		  type: 2,
		  title: "用户信息",
		  closeBtn: 1,
		  shadeClose: true,
		  area: ['603px', '600px'],
		  content: 'userEdit.html'
		});
  	});
  	//批量禁用用户
  	layui.$('#btn_enabled').on('click', function(){
  	 	var checkStatus = table.checkStatus('userID'); 
  	 	if(checkStatus.data.length<=0){
  	 		layer.alert("请至少选中一条数据进行操作!",{icon:5});
  	 		return;
  	 	}
  	 	var selectIds = new Array();
  	 	for(var i=0;i<checkStatus.data.length;i++){
  	 		selectIds.push(checkStatus.data[i].userID);
  	 	}
  	 	setUserEnable(selectIds,false);
  	});
  	//批量恢复用户
  	layui.$('#btn_recovery').on('click', function(){
  	 	var checkStatus = table.checkStatus('userID'); 
  	 	if(checkStatus.data.length<=0){
  	 		layer.alert("请至少选中一条数据进行操作!",{icon:5});
  	 		return;
  	 	}
  	 	var selectIds = new Array();
  	 	for(var i=0;i<checkStatus.data.length;i++){
  	 		selectIds.push(checkStatus.data[i].userID);
  	 	}
  	 	setUserEnable(selectIds,true);
  	});
  	
 });
//刷新列表 
function reloadTable(){
	$("#btn_search").trigger("click");
}

//单个用户恢复和禁用
function chengeUserEnable(userid,status){
	var  selectids = new Array();
	selectids.push(userid);
	setUserEnable(selectids,status);
}

//禁用或恢复用户
function setUserEnable(userids,status){
	var param = {
		userIds:userids,
		status:status
	}
	ajaxSetting({
		url:getServerPath("SETUSERENABLE"),
		data:JSON.stringify(param),
		success:function(res){
			layer.alert("操作成功!",{icon:6});
			reloadTable();
		}
	})
}

//编辑用户信息
function editInfo(id){
	 //页面层-自定义
	layer.open({
	  type: 2,
	  title: "用户信息",
	  closeBtn: 1,
	  shadeClose: true,
	  area: ['603px', '600px'],
	  content: 'userEdit.html',
	  success:function(layero,index){
			// 获取子页面的iframe
            var iframe = window['layui-layer-iframe' + index];
            // 向子页面的全局函数child传参
            iframe.initData(id);
	  }
	});
}

function ResetPassword(id){
    layer.prompt({title: '输入重置后密码，并确认', formType: 1}, function(pass, index){
    	console.log(pass)
    	if(pass.length<6 || pass.length>20){
    		layer.msg("密码必须6到20位，且不能出现空格",{icon:5})
			return;
		}
        ajaxSetting({
            url:getServerPath("RESETPASSWORD"),
            data:JSON.stringify({userID:id,confirmPassword:pass}),
            success:function(res){
                layer.alert("操作成功!",{icon:6});
                reloadTable();
                layer.close(index);
            }
        })
    });
  }
